package com.ch999.detect.View.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.ch999.detect.R;


/**
 * Created by Administrator on 2016/12/14 0014.
 */

public class TouchChangeColorView extends TextView {
    String TAG = this.getClass().getSimpleName();

    private float x;
    private float y;
    private float width;
    private float heith;

    private boolean isTouch;
    private float mStatusBarHeight;

    public interface OnTouchMotionUpListenter {
        public void onTouch(TouchChangeColorView view);
    }

    public OnTouchMotionUpListenter onTouchMotionUpListenter;

    /**
     * 设置移动监听
     *
     * @param onTouchMotionUpListenter
     */
    public void setOnTouchMotionUpListenter(OnTouchMotionUpListenter onTouchMotionUpListenter,float statusBarHeight) {
        this.onTouchMotionUpListenter = onTouchMotionUpListenter;
        mStatusBarHeight = statusBarHeight;
    }


    public TouchChangeColorView(Context context) {
        super(context);
    }

    public TouchChangeColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchChangeColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void inOnTuchEvent(float motion_x, float motion_y) {

        if (isTouch) {
            return;
        }

        if (motion_x > x && motion_x < (x + width) &&
                motion_y > (y-mStatusBarHeight) && motion_y < ((y-mStatusBarHeight) + heith)) {

            setBackgroundResource(R.color.es_g);
            isTouch = true;
            if (onTouchMotionUpListenter != null) {
                onTouchMotionUpListenter.onTouch(this);
            }
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, final int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
            @Override
            public void onDraw() {
                int[] location2 = new int[2];
                getLocationOnScreen(location2);

                x = location2[0];
                y = location2[1];

                width = getWidth();
                heith = getHeight();

            }
        });
    }

}
